package com.quanshi.shopping_cart_customer_api.controller;


import com.quanshi.shopping_common.entity.CartGoods;
import com.quanshi.shopping_common.result.BaseResult;
import com.quanshi.shopping_common.service.ICartService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.Enumeration;
import java.util.List;

/**
 * 购物车管理
 */
@RestController
@RequestMapping("/user/cart")
public class CartController {

    @DubboReference
    private ICartService cartService;

    /**
     * 查询当前登录用户购物车
     * @param userId 用户id
     * @return
     */
    @GetMapping("/findCartList")
    public BaseResult findCartList(@RequestHeader Long userId,HttpServletRequest request){
        List<CartGoods> cartList = cartService.findCartList(userId);
        return BaseResult.success(cartList);
    }

    /**
     * 添加购物车
     * @param userId 用户id
     * @param cartGoods 购物车商品对象
     * @return
     */
    @PostMapping("/addCart")
    public BaseResult addCart(@RequestHeader Long userId ,@RequestBody CartGoods cartGoods){
        cartService.addCart(userId,cartGoods);
        return BaseResult.success();
    }

    /**
     * 修改购物车
     * @param userId 用户id
     * @param goodsId 商品id
     * @param num 购买数量
     * @return
     */
    @GetMapping("/handleCart")
    public BaseResult addCart(@RequestHeader Long userId ,Long goodId,Integer num){
        cartService.handleCart(userId,goodId,num);
        return BaseResult.success();
    }

    /**
     * 删除购物车商品
     * @param userId 用户id
     * @param goodId 商品id
     * @return
     */
    @DeleteMapping("/deleteCart")
    public BaseResult deleteCart(@RequestHeader Long userId,Long goodId){
        cartService.deleteCartOption(userId,goodId);
        return BaseResult.success();
    }
}
